package razor.blade;

import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.beans.value.*;
import javafx.scene.image.*;
import tee.binding.*;

public class ScrollHorizontal {
    private Numeric x;
    private Numeric y;
    private Numeric width;
    private Numeric contentWidth;
    private Numeric shift;
    private Rectangle hov;
    private Group root;
    public Node node() {
        return root;
    }
    public ScrollHorizontal contentWidth(It<Double> it) {
        contentWidth.bind(it);/*.afterChange(new Task(){
	    @Override public void doTask() {
		System.out.println("contentWidth.value(): "+contentWidth.value());
	    }
	});*/
        return this;
    }
    public ScrollHorizontal x(double it) {
        x.value(it);
        return this;
    }
    public ScrollHorizontal x(int it) {
        x.value(it);
        return this;
    }
    public Numeric x() {
        return x;
    }
    public ScrollHorizontal x(It<Double> it) {
        x.bind(it);
        return this;
    }
    public ScrollHorizontal y(double it) {
        y.value(it);
        return this;
    }
    public ScrollHorizontal y(int it) {
        y.value(it);
        return this;
    }
    public Numeric y() {
        return y;
    }
    public ScrollHorizontal y(It<Double> it) {
        y.bind(it);
        return this;
    }
    public ScrollHorizontal width(double it) {
        width.value(it);
        return this;
    }
    public ScrollHorizontal width(int it) {
        width.value(it);
        return this;
    }
    public Numeric width() {
        return width;
    }
    public ScrollHorizontal width(It<Double> it) {
        width.bind(it);
        return this;
    }
    public ScrollHorizontal shift(double it) {
        shift.value(it);
        return this;
    }
    public ScrollHorizontal shift(int it) {
        shift.value(it);
        return this;
    }
    public Numeric shift() {
        return shift;
    }
    public ScrollHorizontal shift(It<Double> it) {
        shift.bind(it);
        return this;
    }
    public ScrollHorizontal() {
        final ScrollHorizontal me = this;
        x = new Numeric().value(0);
        y = new Numeric().value(0);
        width = new Numeric().value(100);
        root = new Group();
        hov = new Rectangle();
        hov.setFill(Color.web("#ff000001"));
        hov.xProperty().bind(new Tee<Double>(x));
        hov.yProperty().bind(new Tee<Double>(y.minus(7)));
        hov.setHeight(16);
        hov.widthProperty().bind(new Tee<Double>(width));
        root.hoverProperty().addListener(new ChangeListener<Boolean>() {
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (root.isHover()) {
                    root.setOpacity(1.0);
                } else {
                    root.setOpacity(0.001);
                }
            }
        });
        root.setOpacity(0.001);
        root.getChildren().add(hov);
        contentWidth = new Numeric().value(1);
        shift = new Numeric().value(0);
        final Numeric _scrollWidth = new Numeric().bind(width.multiply(width).divide(contentWidth).read());
        _scrollWidth.afterChange(new Task() {
                @Override public void doTask() {
		    //System.out.println("_scrollWidth.value(): "+_scrollWidth.value());
                    if (root != null) {
                        if (_scrollWidth != null) {
                            root.setVisible(_scrollWidth.value() < width.value());
                        }
                    }
                }
            });
        final Numeric realScrollWidth = new Numeric().bind(Numeric
                .iF(_scrollWidth.less(38))
                .then(38.0)
                .otherwise(Numeric.iF(_scrollWidth.more(width))
                    .then(38.0)
                    .otherwise(_scrollWidth)));
        root.getChildren().add(new ScrapeHorizontal()
                .x(x)
                .y(y)
                .width(width)
                .node());
        ImageView viewUp = new ImageView();
        ImageView viewMiddle = new ImageView();
        ImageView viewDown = new ImageView();
        viewUp.setImage(new Image(this.getClass().getResourceAsStream("steelHorizontalLeft.png")));
        viewMiddle.setImage(new Image(this.getClass().getResourceAsStream("steelHorizontalMiddle.png")));
        viewDown.setImage(new Image(this.getClass().getResourceAsStream("steelHorizontalRight.png")));
        viewMiddle.setTranslateX(18);
        viewMiddle.fitWidthProperty().bind(new Tee<Double>(realScrollWidth.minus(2 * 18)));
        viewDown.translateXProperty().bind(new Tee<Double>(realScrollWidth.minus(18)));
        Group viewGroup = new Group();
        viewGroup.getChildren().add(viewUp);
        viewGroup.getChildren().add(viewMiddle);
        viewGroup.getChildren().add(viewDown);
        final Tow tow = new Tow();
        root.getChildren().add(tow.onShift(new Task() {
                        @Override public void doTask() {
                            if (tow.shiftX().value() >= me.x().value()) {
                                if (tow.shiftX().value() + realScrollWidth.value() <= me.x().value() + me.width.value()) {
                                    shift.value(tow.shiftX().value() - x.value());
                                }
                            }
                        }
                    })
                .content(viewGroup)
                .y(y.minus(8))
                .x(shift.plus(x))
                .cursor(Cursor.H_RESIZE)
                .node());

    }
}
